let scriptEl; let directBitToken; let directBitAmount; let directBitExternalId; function loadBitCom() { if (scriptEl) { initDirectBitPayment(); } else { let el = document.createElement('script'); el.src = 'https://public.bankhapoalim.co.il/bitcom/sdk'; el.onload = initDirectBitPayment; document.head.append(el); scriptEl = el; } } function initDirectBitPayment() { ShowProcessingDiv(); directBitAmount = Number($('#hdnDonationAmount').val()) + Number(addiotionalSum) + Number($('#hdnDonationCoverCcAmount').val()); fetch('/InitDirectBitPayment', { method: 'POST', headers: {'Content-Type': 'application/json'}, body: JSON.stringify({ govId: _govId, amount: directBitAmount, email: $("#tbcontactemail").val(), pfpId: $("#hdnPfpId").val() }) }) .then(response => response.json()) .then(function (data) { if (data.isFail) { throw new Error('Something went wrong. Please contact our support.'); } directBitToken = data.Token; directBitExternalId = data.ExternalPaymentId; let paymentData = { transactionSerialId: data.TransactionSerialId, paymentInitiationId: data.PaymentInitiationId, onApproved: checkStateDirectBitPaymentAndGoForward, onCancel: markDirectBitPaymentAsDeleted, onTimeout: markDirectBitPaymentAsDeleted, log: function (data) { if(data.description === 'payment-modal-closed') { HideProcessingDiv(); // to handle the case when open bit and close the popup using the x on the side } console.log(data); } }; pay(paymentData); }) .catch(error => { console.log(error); HideProcessingDiv(); alert(error.message); }); } function pay(paymentData) { if (BitPayment) { BitPayment.pay(paymentData); } else { alert('Something went wrong. Object BitPayment is undefined.') } } function checkStateDirectBitPaymentAndGoForward(paymentData) { fetch('/CheckStateDirectBitPayment', { method: 'POST', headers: {'Content-Type': 'application/json'}, body: JSON.stringify({ govId: _govId, paymentInitiationId: paymentData.allData.orderId, token: directBitToken, pfpId: $("#hdnPfpId").val() }) }) .then(response => response.json()) .then(function (data) { console.log(data); if (data.isFail) { throw new Error('Something went wrong. Please contact our support.'); } $("#hdnDirectBitPaymentInitiationId").val(paymentData.allData.orderId); $("#hdnDirectBitToken").val(directBitToken); $("#hdnDirectBitExternalId").val(directBitExternalId); SubmitPage(); }) .catch(error => { console.log(error); HideProcessingDiv(); alert(error.message); }); } function markDirectBitPaymentAsDeleted(paymentData) { fetch('/MarkDirectBitPaymentAsDeleted', { method: 'POST', headers: {'Content-Type': 'application/json'}, body: JSON.stringify({ govId: _govId, paymentInitiationId: paymentData.allData.orderId, token: directBitToken, pfpId: $("#hdnPfpId").val() }) }) .then(response => response.json()) .then(function (data) { console.log(data); if (data.isFail) { throw new Error('Something went wrong. Please contact our support.'); } HideProcessingDiv(); alert('Payment has been cancelled!'); }) .catch(error => { console.log(error); HideProcessingDiv(); alert(error.message); }); } function ShowProcessingDiv() { $("#preparing").show(); $("#btnDonateButton").hide(); } function HideProcessingDiv() { $("#preparing").hide(); $("#btnDonateButton").show(); } function getPlatform() { const userAgent = navigator.userAgent || navigator.vendor; // Windows Phone must come first because its UA also contains "Android" if (/windows phone/i.test(userAgent)) { return "Desktop"; } if (/android/i.test(userAgent)) { return "Android"; } // iOS detection from: http://stackoverflow.com/a/9039885/177710 if (/iPad|iPhone|iPod/.test(userAgent) && !window.MSStream) { return "iOS"; } return "Desktop"; } function isPageInIframe() { try { // Return true if the page is not the topmost window or if window.parent is the same as window.self, we are not in an iframe return window.self !== window.top || window.parent !== window.self; } catch (e) { // If access to window.top is denied due to cross-origin policy, // assume the page is inside an iframe. return true; } }